package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.maintain.MaintainMonthReportDTO;
import com.whfc.mach.dto.maintain.MaintainPayStatDTO;
import com.whfc.mach.dto.maintain.MaintainYearReportDTO;
import com.whfc.ms.api.mach.service.MsMachMaintainReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 设备保养报表
 *
 * @author sun_guodong
 * @date 2021-08-13
 */
@RequiresPermissions("mach/maintain/report")
@RestController
@RequestMapping("/ms/api/v1/mach/maintain")
public class MsMachMaintainReportController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachMaintainReportService machMaintainReportService;

    @GetMapping(value = "/maintainPayStat", params = {"startDate"})
    public Result maintainPayStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("deptId") Integer deptId,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate,
                                  @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("设备保养费用(按月)统计,loginer:{},deptId:{},startDate:{},endDate:{},keyword:{}",
                user.getNickname(), deptId, startDate, endDate, keyword);
        MaintainPayStatDTO data = machMaintainReportService.maintainPayStat(deptId, startDate, endDate, keyword);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/maintainPayStat", params = {"year"})
    public Result maintainPayStat(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("deptId") Integer deptId,
                                  @RequestParam("year") Integer year,
                                  @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("设备保养费用(按年)统计,loginer:{},deptId:{},year:{},keyword:{}",
                user.getNickname(), deptId, year, keyword);
        Date startDate = DateUtil.getYearBegin(year);
        Date endDate = DateUtil.getYearEnd(year);
        MaintainPayStatDTO data = machMaintainReportService.maintainPayStat(deptId, startDate, endDate, keyword);
        return ResultUtil.success(data);
    }

    @GetMapping("/reportByMonth")
    public Result reportByMonth(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate,
                                @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("设备保养月报,loginer:{},deptId:{},startDate:{},endDate:{},keyword:{}",
                user.getNickname(), deptId, startDate, endDate, keyword);
        MaintainMonthReportDTO data = machMaintainReportService.reportByMonth(deptId, startDate, endDate, keyword);
        return ResultUtil.success(data);
    }

    @GetMapping("/reportByYear")
    public Result reportByYear(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("deptId") Integer deptId,
                               @RequestParam("year") Integer year,
                               @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("设备保养年报,loginer:{},deptId:{},year:{},keyword:{}",
                user.getNickname(), deptId, year, keyword);
        MaintainYearReportDTO data = machMaintainReportService.reportByYear(deptId, year, keyword);
        return ResultUtil.success(data);
    }

    @PostMapping(value = "/report/export", params = "startDate")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate,
                         @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("导出保养月报,loginer:{},deptId:{},startDate:{},endDate:{},keyword:{}",
                user.getNickname(), deptId, startDate, endDate, keyword);
        machMaintainReportService.export(deptId, startDate, endDate, keyword);
        return ResultUtil.success();
    }

    @PostMapping(value = "/report/export", params = "year")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam("year") Integer year,
                         @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("导出保养年报,loginer:{},deptId:{},year:{},keyword:{}",
                user.getNickname(), deptId, year, keyword);
        machMaintainReportService.export(deptId, year, keyword);
        return ResultUtil.success();
    }

}
